<?php
namespace app\know\validate;

use app\know\model\TeachingMaterialGrammarModel;
use think\Validate;
use think\facade\Log;

class TeachingMaterialGrammarValidate extends Validate
{
    protected $rule = [
        'material_uuid' => 'require|length:32',
        'chapter_uuid' => 'require|length:32',
        'grammar_uuid' => 'require|length:32|checkKey',
        'id' => 'require|gt:0',
    ];

    protected $message = [
        'material_uuid.require' => '教材编码为必填项',
        'material_uuid.length' => '教材编码长度不合要求',
        'chapter_uuid.require' => '章节为必填项',
        'chapter_uuid.length' => '章节长度不合要求',
        'grammar_uuid.require' => '语法为必填项',
        'grammar_uuid.length' => '语法长度不合要求',
        'id.require' => 'id不能为空',
    ];

    protected $scene = [
        'add'  => ['material_uuid', 'chapter_uuid' , 'grammar_uuid'],
        'delete' => ['id'],
    ];

    protected function checkKey($value, $rule, $data)
    {
        $model = new TeachingMaterialGrammarModel();
        $item  = $model->where('material_uuid', $data['material_uuid'])
                       ->where('chapter_uuid', $data['chapter_uuid'])
                       ->where('grammar_uuid', $data['grammar_uuid'])
                       ->find();

        if (empty($item))
        {
            return true;
        } 
        else
        {
            return $data['grammar_uuid'] . "---已经存在---";
        }
    }
}